﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using Dogshow.Model;
using DogshowRepository;
using System.Linq;

namespace Dogshow.Linq2SQLRepository
{
    public class PsiRepository : RepositoryBase, IPsiRepository
    {
        public int Sacuvaj(Model.Pas pas)
        {
            using (var model = new DogshowModel())
            {
                Psi p;
                if (pas.Id == 0)
                {
                    p = ModelMapper.Map(pas);
                    model.Psis.InsertOnSubmit(p);
                    model.SubmitChanges();
                }
                else
                {
                    p = model.Psis.Single(x => x.Id == pas.Id);
                    ModelMapper.Map(pas, p);
                    model.SubmitChanges();
                    //model.ExecuteCommand("DELETE FROM TitulePasa WHERE IdPsa={0}", pas.Id);
                }

                //foreach (var titula in pas.Titule)
                //{
                //    var t = ModelMapper.Map(titula);
                //    t.IdPsa = pas.Id;
                //    model.TitulePasas.InsertOnSubmit(t);
                //}
                //model.Submit();
                return p.Id;
            }
        }

        public IEnumerable<Model.Pas> VratiSve()
        {
            using (var model = new DogshowModel())
            {
                var dlo = new DataLoadOptions();
                dlo.LoadWith<Pas>(x => x.RasaPsa);
                model.LoadOptions = dlo;
                return model.Psis.Select(x => ModelMapper.Map(x)).ToList();    
            }
        }

        public Pas VratiPoId(int id)
        {
            using (var model = new DogshowModel())
            {
                var pas = model.Psis.SingleOrDefault(x => x.Id == id);
                if (pas == null) return null;
                return ModelMapper.Map(pas);    
            }
            
        }

        public IEnumerable<InfoOPsu> VratiInfoOPsima()
        {
            using (var model = new DogshowModel())
            {
                var lo = new DataLoadOptions();
                lo.LoadWith<Psi>(x => x.RasePasa);
                lo.LoadWith<RasaPsa>(x => x.FciGrupa);
                model.LoadOptions = lo;
                return model.Psis.Select(x => ModelMapper.MapirajInfoOPsu(x)).ToArray();
            }
        }

        public bool PostojiPas(string brojRodovnika, int idPsa)
        {
            using (var model = new DogshowModel())
            {
                return model.Psis.Any(x => x.BrojRodovnika == brojRodovnika && x.Id != idPsa);
            }
        }

        public Pas VratiPsaPoBrojuRodovnika(string brojRodovnika)
        {
            using (var model = new DogshowModel())
            {
                var pas = model.Psis.SingleOrDefault(x => x.BrojRodovnika == brojRodovnika);
                if (pas == null) return null;
                return ModelMapper.Map(pas);
            }
        }

        public IEnumerable<Pas> PretraziPse(string ime, int? idRase, int? idFciGrupe, int? idVlasnika, int? idOdgajivaca)
        {
            using (var model = new DogshowModel())
            {
                var psi = model.Psis.AsQueryable();
                if (!String.IsNullOrEmpty(ime))
                {
                    psi = psi.Where(x => x.ImePsa.Contains(ime));
                }
                if (idRase.HasValue && idRase != 0)
                {
                    psi = psi.Where(x => x.IdRase == idRase);
                }
                if (idFciGrupe.HasValue && idFciGrupe != 0)
                {
                    psi = psi.Where(x => x.RasePasa.FCIGrupe.Id == idFciGrupe);
                }
                if (idVlasnika.HasValue && idVlasnika != 0)
                {
                    psi = psi.Where(x => x.IdVlasnika == idVlasnika);
                }
                if (idOdgajivaca.HasValue && idOdgajivaca != 0)
                {
                    psi = psi.Where(x => x.IdOdgajivaca == idOdgajivaca);
                }

                return psi.Select(ModelMapper.Map).ToArray();
            }
        }

        public IEnumerable<TitulaPsa> VratiTitulePsa(int idPsa)
        {
            using (var model = new DogshowModel())
            {
                return model.TitulePasas.Where(x => x.IdPsa == idPsa).Select(ModelMapper.Map).ToArray();
            }
        }

        public IEnumerable<Pas> FiltrirajPoBrojuRodovnika(string filter, int beginIndex, int endIndex)
        {
            using (var model = new DogshowModel())
            {
                var svi = model.Psis.AsQueryable();
                if (!String.IsNullOrEmpty(filter))
                {
                    svi = svi.Where(x => x.BrojRodovnika.Contains(filter));
                }
                return svi.Skip(beginIndex).Take(endIndex - beginIndex + 1).Select(ModelMapper.Map).ToArray();
            }
        }

        public IEnumerable<Pas> VratiOmiljenePseKorisnika(string korisnickoIme)
        {
            using (var model = new DogshowModel())
            {
                var lo = new DataLoadOptions();
                lo.LoadWith<OmiljeniPsi>(x => x.Psi);
                //lo.LoadWith<Pas>(x => x.RasaPsa);
                //lo.LoadWith<RasaPsa>(x => x.FciGrupa);
                model.LoadOptions = lo;
                return model.OmiljeniPsis.Where(x => x.KorisnickoIme == korisnickoIme)
                                          .Select(x => ModelMapper.Map(x.Psi))
                                          .ToArray();
            }
        }

        public void SacuvajPsaUOmiljene(Pas pas, string korisnickoIme)
        {
            using (var model = new DogshowModel())
            {
                if (pas.Id == 0)
                {
                    model.OmiljeniPsis.InsertOnSubmit(new OmiljeniPsi()
                    {
                        Psi = ModelMapper.Map(pas),
                        KorisnickoIme = korisnickoIme
                    });    
                } else
                {
                    var postojeci = model.Psis.Single(x => x.Id == pas.Id);
                    ModelMapper.Map(pas, postojeci);
                }
                model.Submit();
            }
        }

        public IEnumerable<Pas> VratiPseKinologa(int idKinologa)
        {
            using (var model = new DogshowModel())
            {
                return model.Psis.Where(x => x.IdVlasnika == idKinologa).Select(ModelMapper.Map).ToArray();
            }
        }
    }
}